const http = require("http");
const fs = require("fs");
const path = require("path");

http.createServer((req, res) => {
    let str = ""
    console.log(req.url);
    if(req.url.indexOf("/login") >= 0) {
        let fileContent = fs.readFileSync(path.join(__dirname , "data.json"));
        console.log(JSON.parse(fileContent.toString()));
        let arr = JSON.parse(fileContent.toString());
        req.on("data" , (chunk) => {
            str += chunk;
        })
        req.on("end" , () => {
            console.log(JSON.parse(str));
            let obj = JSON.parse(str);
            console.log(arr.find(item => item.username === obj.username && item.password === obj.password));
            res.end(arr.find(item => item.username === obj.username && item.password === obj.password) ? "登陆成功" : "用户名或密码错误");
        })
        // res.end("123456");
    }
}).listen(3000);